package com.yaozheng.vocationaltraining.view.question.item;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.ItemQuestionView;

import net.sf.json.JSONArray;

import org.androidannotations.annotations.CheckedChange;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

/**
 * 一项多选
 */
@EViewGroup(R.layout.item_multiple_choice_question)
public class ItemMultipleChoiceQuestionView extends ItemBaseQuestionView {
    @ViewById
    CheckBox optionsCheck1;
    @ViewById
    View optionsImage1, itemQuestionBorderView;


    public ItemMultipleChoiceQuestionView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ItemMultipleChoiceQuestionView(Context context) {
        super(context);
    }

    @Override
    public void setPosition(int position) {
        if (position == 0) {
            itemQuestionBorderView.setVisibility(View.INVISIBLE);
        } else {
            itemQuestionBorderView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void setOptionsContent(String optionsContent) {
        optionsCheck1.setText(optionsContent);
    }

    @CheckedChange({R.id.optionsCheck1})
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            String answer = TypeUtils.getJsonString(itemData, "answer");
            if (answer == null || "".equals(answer)) {
                itemData.put("answer", answerCode);
                itemData.put("questionStatus", 1);
            } else {
                if (answer.indexOf(answerCode) == -1) {
                    itemData.put("answer", answer + "," + answerCode);
                }
            }

        } else {
            if (buttonView == optionsCheck1) {
                if (optionsImage1.getVisibility() == View.VISIBLE) {
                    optionsCheck1.setChecked(true);
                    return;
                }
            }
            String answer = TypeUtils.getJsonString(itemData, "answer");
            if (answer != null && !"".equals(answer)) {
                answer = answer.replace(answerCode + ",", "").replace(answerCode, "");
                itemData.put("answer", answer);

            }
            if (answer == null || "".equals(answer)) {
                itemData.put("questionStatus", 0);
            }
        }
    }

    @Override
    public void optionsCard(JSONArray optionsCard) {
        optionsImage1.setVisibility(View.GONE);
        if (optionsCard != null) {
            for (int i = 0; i < TypeUtils.getJsonArraySize(optionsCard); i++) {
                String optionCard = TypeUtils.getJsonString(optionsCard, i);
                if (answerCode.equals(optionCard)) {
                    optionsImage1.setVisibility(View.VISIBLE);
                    optionsCheck1.setChecked(true);

                }
            }
        }
    }

    @Override
    public void setAnswer(String answer) {
        if (answer != null && !"".equals(answer)) {
            if (answer.indexOf(answerCode) != -1) {
                optionsCheck1.setChecked(true);
                return;
            }
        }
        optionsCheck1.setChecked(false);

    }
}
